import scrapy
from yesnogame.items import PuzzleItem


class SituationpuzzlespiderSpider(scrapy.Spider):
    name = "SituationpuzzlespiderSpider"
    allowed_domains = ['yesnogame.net']
    start_urls = ["https://yesnogame.net/en/stories/157"]

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.urls = open('src.txt').read().split('\n')
        self.index = 0

    def parse(self, response):
        puzzleItem = PuzzleItem()
        res = response.css('div[class="quest__story__text"] p::text')
        if len(res) != 2:
            yield puzzleItem
        else:
            puzzleItem['question'] = res.extract()[0]
            puzzleItem['answer'] = res.extract()[1]
            yield puzzleItem
        self.index += 1
        if self.index == len(self.urls) - 1:
            yield scrapy.Request(url='', callback=self.parse)
        yield scrapy.Request(url=self.urls[self.index], callback=self.parse)
